// ==UserScript==
// @name           Category Sort Event Fix
// @namespace      http://jira.secondlife.com/browse/WEB-533
// @description    Fixes the category sort bug on SecondLife.com/events/
// @include        http://secondlife.com/events/*
// ==/UserScript==
// Written by SignpostMarv Martin http://blog.signpostmarv.name/

var sl_event_category_sort_form = document.getElementsByName('SelectCategory')[0];
function sl_event_category_sort_form_submit()
{
	document.getElementsByName('SelectCategory')[0].submit();
}
function addEventSimple(obj,evt,fn){if(obj.addEventListener)obj.addEventListener(evt,fn,false);else if(obj.attachEvent)obj.attachEvent('on'+evt,fn)}
if(sl_event_category_sort_form)
{
	var input = document.createElement('input');
		input.type = 'hidden';
		input.name = 'date'
		input.value = sl_event_category_sort_form.getElementsByTagName('input')[0].value;
	var sl_event_category_sort_form_select = sl_event_category_sort_form.getElementsByTagName('select')[0];
		sl_event_category_sort_form_select.removeAttribute('onchange');
	var sl_event_category_sort_form_replacement = document.createElement('form');
		sl_event_category_sort_form_replacement.appendChild(input);
		sl_event_category_sort_form_replacement.appendChild(document.createTextNode('Category:'));
		sl_event_category_sort_form_replacement.appendChild(sl_event_category_sort_form_select);
		sl_event_category_sort_form_replacement.action = 'http://secondlife.com/events/';
		sl_event_category_sort_form_replacement.method = 'GET';
		sl_event_category_sort_form_replacement.name = 'SelectCategory';
		addEventSimple(sl_event_category_sort_form_replacement.getElementsByTagName('select')[0],'change',sl_event_category_sort_form_submit);
	var sl_event_category_sort_form_div = document.createElement('div');
		sl_event_category_sort_form_div.setAttribute('style','float:right');
		sl_event_category_sort_form_div.appendChild(sl_event_category_sort_form_replacement);
	var sl_event_category_sort_form_parent = sl_event_category_sort_form.parentNode;
		sl_event_category_sort_form_parent.removeChild(sl_event_category_sort_form);
		sl_event_category_sort_form_parent.appendChild(sl_event_category_sort_form_div);
}